﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {
    /// <summary>
    /// Interaction logic for TeamPanel.xaml
    /// </summary>
    public partial class TeamPanel : GenericPanel  {

        public TeamPanel() {
            InitializeComponent();            
        }


        private string OldClientName { get; set; }
        
        protected override bool RequestData() {            
            LoadClientContacts();
            return true;
        }



        public void LoadClientContacts(string clientName) {
            
            ObjectDataProvider clientContacts = (ObjectDataProvider)FindResource("ClientContactsList");

            clientContacts.ObjectInstance = Client.RetriveProjectProductOwners(clientName);

            OldClientName = clientName;

            UpdateLayout();
        }


        public void LoadClientContacts() {
            if (Project == null)
                return;
            LoadClientContacts(Project.ClientName);
        }

        

        private void Delete_Executed(object sender, ExecutedRoutedEventArgs e) {
            Data.ProjectDataSet.ProjectRoleDataTable roles = DataContextAsProjectDataSet.ProjectRole;            
            Button b = e.OriginalSource as Button;
            Data.ProjectDataSet.ProjectRoleRow role = Helper.GetRowFromRowView<Data.ProjectDataSet.ProjectRoleRow>(b.DataContext);
            if (role == null)
                return;

            role.Delete();

        }


        /// <summary>
        /// When the menuitem is clicked, adds a new member.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addMember_Click(object sender, RoutedEventArgs e) {
            // figure out the item clicked
            MenuItem item = e.OriginalSource as MenuItem;
            Data.ProjectDataSet.BacklogItemUnitRow unit = Helper.GetRowFromRowView<Data.ProjectDataSet.BacklogItemUnitRow>(item.DataContext);

            // adds the new member
            AddNewMember(unit);

            e.Handled = true;
            
        }

        /// <summary>
        /// Adds the new member using a give unit.
        /// </summary>
        /// <param name="unit">The new member unit</param>
        private void AddNewMember(Data.ProjectDataSet.BacklogItemUnitRow unit) {

            // gets the datasource
            Data.ProjectDataSet dsProject = DataContextAsProjectDataSet;
            
            BindingListCollectionView bindingList = teamListView.ItemsSource as BindingListCollectionView;

            System.Data.DataRowView added = (System.Data.DataRowView)bindingList.AddNew();

            added["projectUId"] = dsProject.Project[0].ProjectUId;
            added["ContactId"] = String.Empty;
            added["ContactEmail"] = String.Empty;
            added["ContactName"] = String.Empty;
            added["ProjectRoleUId"] = Guid.NewGuid().ToString().ToLower();
            added["UnitName"] = unit.UnitName;
            added["ClientContactRole"] = (unit.Type != (short)TeamRoles.CLIENT_CONTACT);

            bindingList.CommitNew();

            teamListView.UpdateLayout();

            teamListView.ScrollIntoView(added);            
            teamListView.SelectedItem = added;

            ListBoxItem item = teamListView.ItemContainerGenerator.ContainerFromItem(teamListView.SelectedItem) as ListBoxItem;
            BadHabit.WPFTools.ComboBox combo = Helper.GetObjectOfTypeInVisualTree<BadHabit.WPFTools.ComboBox>(item);            
            combo.IsDropDownOpen = true;
            combo.Focus();
           
          
           
           
        }

       

     

        private void editRoleButton_Click(object sender, RoutedEventArgs e) {
            if (Client.ShowEditRoleWindow(DataContextAsProjectDataSet, false)) {
                BindingListCollectionView bindingList = teamListView.ItemsSource as BindingListCollectionView;
                bindingList.Refresh();
            }
        }

        /// <summary>
        /// When the selected member changes copies to member data to the project role.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void memberComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {            

            // no member selected, get out of my luckin face
            if (e.AddedItems.Count == 0)
                return;            
            Data.TeamDataSet.MemberRow member = Helper.GetRowFromRowView<Data.TeamDataSet.MemberRow>(e.AddedItems[0]);
            if (member == null)
                return;

            // could not find role, get out of my luckin face
            Control c = sender as Control;
            if (c == null)
                return;
            Data.ProjectDataSet.ProjectRoleRow role = Helper.GetRowFromRowView<Data.ProjectDataSet.ProjectRoleRow>(c.DataContext);
            if (role == null)
                return;

            // if the e-mail has changed, updated
            if(!member.IsEmailAccountNull() && !member.EmailAccount.Equals(role.ContactEmail))
                role.ContactEmail = member.EmailAccount;

            // if the name has changed, updated
            if(!member.NickName.Equals(role.ContactName))
                role.ContactName = member.NickName;            
            
        }

        private void GenericPanel_Loaded(object sender, RoutedEventArgs e) {
            if (Project == null)
                return;
            if(!Project.ClientName.Equals(OldClientName))
                LoadClientContacts();
        }

       
        private void memberComboBox_Loaded(object sender, RoutedEventArgs e) {
            ComboBox combo = sender as ComboBox;            
            combo.GetBindingExpression(ComboBox.SelectedValueProperty).UpdateTarget();            
        }

        

        


    }


}
